package com.zzh.mybatis.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.zzh.mybatis.po.User;

/**
 * UserDaoImpl.java
 * 
 * @author zhangzhenhua
 * @date 2016年9月28日 下午7:12:59
 * @description TODO
 */
public class UserDaoImpl implements UserDao {

	// 向dao实现类中注入SqlSessionFactory
	// 通过函数注入
	private SqlSessionFactory ssf;

	public UserDaoImpl(SqlSessionFactory ssf) {
		this.ssf = ssf;
	}

	@Override
	public User findUserById(int id) throws Exception {
		SqlSession ss = ssf.openSession();

		return ss.selectOne("test.findUserById", id);
	}

	@Override
	public void insertUser(User user) throws Exception {
		// TODO Auto-generated method stub
		SqlSession ss = ssf.openSession();
		ss.insert("test.insertUser",user);
		ss.commit();
		ss.close(); 

	}

	@Override
	public void deleteUser(int id) throws Exception {
		// TODO Auto-generated method stub
		SqlSession ss = ssf.openSession();
		ss.delete("test.deleteUser",id);
		ss.commit();
		ss.close();

	}

	@Override
	public void updateUser(User user) throws Exception {
		// TODO Auto-generated method stub
		SqlSession ss = ssf.openSession();
		ss.update("test.updateUser",user);
		ss.commit();
		ss.close(); 

	}

}
